﻿//This file is part of Search Engine by Tomasz Kusienicki.
// 
//This Search Engine is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.
// 
//Foobar is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
// 
//You should have received a copy of the GNU General Public License
//on <http://www.gnu.org/licenses/>.

using System;
using System.Security.Principal;
using System.Windows.Forms;
using AdminApp1.Common;
using AdminApp1.Models;
using AdminApp1.Views;

namespace AdminApp1.Presenters
{
    public class DataControlPresenter : PresenterBase<DataControlView, DataControlPresenter, DataControlModel>
    {
        private readonly MainPresenter _mainPresenter;

        #region Constructor
        public DataControlPresenter(DataControlView view, DataControlModel model, MainPresenter mainPresenter) 
            : base(view, model)
        {
            _mainPresenter = mainPresenter;
            Model.BusDetailsModel = new BusDetailsModel();
            Model.BusLineModel = new BusLineModel();
            Model.BusStopModel = new BusStopModel();
            Model.MapModel = new MapModel();
            Model.StepDetailsModel = new StepDetailsModel();
            Model.StepModel = new StepModel();
        }
        #endregion


        #region HandleFormClosing
        public void HandleDataControlFormClosing(object sender, FormClosingEventArgs e)
        {
            _mainPresenter.HandleDisconnect();
            Environment.Exit(0);
        }
        #endregion

        #region Handle: Connected, Disconnect
        public void HandleConnected()
        {
            Model.SearchEngineDatabaseDataContext = new SearchEngineDatabaseDataContext();

            View.Text = _mainPresenter.Model.ApplicationName;
            if(_mainPresenter.Model.TrustedConnection)
            {
                var windowsIdentity = WindowsIdentity.GetCurrent();
                if (windowsIdentity == null) return;
                View.lblUser.Text = @"User: " + windowsIdentity.Name;
            }
            else
            {
                View.lblUser.Text = @"User: " + _mainPresenter.Model.User;
            }
            View.Show();
        }

        public void HandleDisconnect()
        {
            View.Hide();
            if (Model.BusDetailsPresenter != null)
            {
                Model.BusDetailsPresenter.HandleOnClosed();
                Model.BusDetailsPresenter = null;
            }
            if (Model.BusLinePresenter != null)
            {
                Model.BusLinePresenter.HandleOnClosed();
                Model.BusLinePresenter = null;
            }
            if (Model.BusStopPresenter != null)
            {
                Model.BusStopPresenter.HandleOnClosed();
                Model.BusStopPresenter = null;
            }
            if (Model.MapPresenter != null)
            {
                Model.MapPresenter.HandleOnClosed();
                Model.MapPresenter = null;
            }
            if (Model.StepDetailsPresenter != null)
            {
                Model.StepDetailsPresenter.HandleOnClosed();
                Model.StepDetailsPresenter = null;
            }
            if (Model.StepPresenter != null)
            {
                Model.StepPresenter.HandleOnClosed();
                Model.StepPresenter = null;
            }
            Model.SearchEngineDatabaseDataContext = null;
            _mainPresenter.HandleDisconnect();
            _mainPresenter.View.Show();
        }
        #endregion

        #region HandleButtonClick
        public void HandleBusDetailsClick()
        {
            if (Model.BusDetailsPresenter == null)
            {
                Model.BusDetailsPresenter = new BusDetailsPresenter(new BusDetailsView(), Model.BusDetailsModel, Model.SearchEngineDatabaseDataContext);
                Model.BusDetailsPresenter.Initialize();
                Model.BusDetailsPresenter.View.Show();
            }
            else
            {
                Model.BusDetailsPresenter.View.Show();
                Model.BusDetailsPresenter.View.BringToFront();
            }
        }

        public void HandleBusLineClick()
        {
            if (Model.BusLinePresenter == null)
            {
                Model.BusLinePresenter = new BusLinePresenter(new BusLineView(), Model.BusLineModel, Model.SearchEngineDatabaseDataContext);
                Model.BusLinePresenter.Initialize();
                Model.BusLinePresenter.View.Show();
            }
            else
            {
                Model.BusLinePresenter.View.Show();
                Model.BusLinePresenter.View.BringToFront();
            }
        }

        public void HandleBusStopClick()
        {
            if (Model.BusStopPresenter == null)
            {
                Model.BusStopPresenter = new BusStopPresenter(new BusStopView(), Model.BusStopModel, Model.SearchEngineDatabaseDataContext);
                Model.BusStopPresenter.Initialize();
                Model.BusStopPresenter.View.Show();
            }
            else
            {
                Model.BusStopPresenter.View.Show();
                Model.BusStopPresenter.View.BringToFront();
            }
        }

        public void HandleMapClick()
        {
            if (Model.MapPresenter == null)
            {
                Model.MapPresenter = new MapPresenter(new MapView(), Model.MapModel, Model.SearchEngineDatabaseDataContext);
                Model.MapPresenter.Initialize();
                Model.MapPresenter.View.Show();
            }
            else
            {
                Model.MapPresenter.View.Show();
                Model.MapPresenter.View.BringToFront();
            }
        }

        public void HandleStepDetailsClick()
        {
            if (Model.StepDetailsPresenter == null)
            {
                Model.StepDetailsPresenter = new StepDetailsPresenter(new StepDetailsView(), Model.StepDetailsModel, Model.SearchEngineDatabaseDataContext);
                Model.StepDetailsPresenter.Initialize();
                Model.StepDetailsPresenter.View.Show();
            }
            else
            {
                Model.StepDetailsPresenter.View.Show();
                Model.StepDetailsPresenter.View.BringToFront();
            }
        }

        public void HandleStepClick()
        {
            if (Model.StepPresenter == null)
            {
                Model.StepPresenter = new StepPresenter(new StepView(), Model.StepModel, Model.SearchEngineDatabaseDataContext);
                Model.StepPresenter.Initialize();
                Model.StepPresenter.View.Show();
            }
            else
            {
                Model.StepPresenter.View.Show();
                Model.StepPresenter.View.BringToFront();
            }
        }
        #endregion
    }
}
